package com.example.tic_tac_toe_in_android.model;

import java.util.Observable;
import java.util.Scanner;

import android.text.method.MovementMethod;
import android.util.Log;

public class TicTacToeGame extends Observable {

	private static TicTacToeGame instance;
	private Player player1;
	private Player player2;
	private Player winnerPlayer;
	public Board board;
	public Referee referee;
	private Player currentPlayer;

	public TicTacToeGame(Player player1, Player player2) {
		System.out.println("TicTacToeGame.TicTacToeGame()");

		this.player1 = player1;
		this.player2 = player2;
		player1.setCross(true);
		player2.setCross(false);
		winnerPlayer = null;
		board = new Board();
		referee = new Referee(board);
		player1.setMyTurn(true);
		player2.setMyTurn(false);

	}

	// private TicTacToeGame(Player player1, Player player2) {
	// this.player1 = player1;
	// this.player2 = player2;
	// player1.setCross(true);
	// player2.setCross(false);
	// winnerPlayer = null;
	// board = new Board();
	// referee = new Referee(board);
	// player1.setMyTurn(true);
	// player2.setMyTurn(false);
	// }
	//
	// public static TicTacToeGame getInstance(Player player1, Player player2) {
	// if (instance == null) {
	// instance = new TicTacToeGame(player1, player2);
	// }
	// return instance;
	// }

	public void Move(Position position) {

		if (position != null) {

			board.displayBoard();

			if (!referee.isWin() & !referee.isDraw()) {

				if (player1.isMyTurn()) {
					currentPlayer = player1;
					System.out.println("Player 1 ... plz play");

					if ((position.getCoords()[0] < 3)
							& (position.getCoords()[1] < 3)) {

						if (board.isSlotFree(position)) {
							XPiece xPiece = new XPiece(position);
							board.addPiece(xPiece);
							player1.setMyTurn(false);
							player2.setMyTurn(true);
						} else {
							System.out
									.println("THIS slot is not Free... Try Again");
						}
					} else {
						System.out.println("wrong position... Try Again");
					}

					if (referee.isWin()) {
						winnerPlayer = player1;
						player1.hasWon();
						System.out
								.println("=============Player1 Wins=============");
					} else if (referee.isDraw()) {
						System.out.println("DRAAAAAAAAAAAAAAAAWWWWWW");

					}

				} else if (player2.isMyTurn()) {
					currentPlayer = player2;
					System.out.println("Player 2 ... plz play");

					if ((position.getCoords()[0] < 3)
							& (position.getCoords()[1] < 3)) {

						if (board.isSlotFree(position)) {
							OPiece oPiece = new OPiece(position);
							board.addPiece(oPiece);
							player1.setMyTurn(true);
							player2.setMyTurn(false);
						} else {
							System.out
									.println("THIS slot is not Free... Try Again");
						}

					} else {
						System.out.println("wrong position... Try Again");
					}
					if (referee.isWin()) {
						winnerPlayer = player2;
						player2.hasWon();
						System.out
								.println("=============Player2 Wins=============");
					} else if (referee.isDraw()) {
						System.out.println("DRAAAAAAAAAAAAAAAAWWWWWW");

					}

				} else {
					System.out.println("ERRRRRRRRRRRRRRRRRRRRRRRRRRRROR");
				}
				board.displayBoard();
				updateGame();

			}
		}
	}

	/*
	 * returns the player who Won
	 */
	public Player getWinnerPlayer() throws NullPointerException {
		return winnerPlayer;

	}

	/*
	 * returns the current player type which is cross or not
	 */
	public boolean isCurrentPlayerTypeCross() {
		return currentPlayer.isCross();

	}

	/*
	 * reset the tictactoe Game
	 */
	public void reset() {
		// winnerPlayer.setName("");
		winnerPlayer = null;
		board = new Board();
		// board.resetBoard();
		referee = new Referee(board);
		player1.setMyTurn(true);
		player2.setMyTurn(false);
		updateGame();

	}

	public void updateGame() {
		setChanged();
		notifyObservers();

	}

}
